package com.example.tianxiaoyu.zyfypt__128txy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.tianxiaoyu.zyfypt__128txy.R;
import com.example.tianxiaoyu.zyfypt__128txy.bean.LoginBean;
import com.example.tianxiaoyu.zyfypt__128txy.listener.LoginListener;
import com.example.tianxiaoyu.zyfypt__128txy.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn,btn1;
    private EditText et1,et2;
    private Switch sw;
    private String username="",password="",sessionid="",realname="",rolename="";//用户名，密码
    private SharedPreferences sp;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:"+loginBean.getSessionid());
            if(loginBean.getSessionid()!=null) {
                sessionid=loginBean.getSessionid().toString();
                realname=loginBean.getRealname().toString();
                rolename=loginBean.getRolename();
                username=loginBean.getUsername();
                System.out.println("----username="+username);
                System.out.println("-----rolename="+rolename);
                System.out.println("----realname="+realname);
                saveSP();
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this,"登录",Toast.LENGTH_SHORT).show();
        init();
        //实例化sp
        sp=getSharedPreferences("login",MODE_PRIVATE);
        //读取sp
        readSP();
    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        String session=sp.getString("sessionid",null);
        String realname=sp.getString("realname",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            et1.setText(name);
            et2.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init() {
        btn=(Button)findViewById(R.id.buttondl);
        btn1=(Button)findViewById(R.id.buttonzc);
        et1=(EditText) findViewById(R.id.editTextname);
        et2=(EditText) findViewById(R.id.editTextpass);
        sw=(Switch) findViewById(R.id.switch1);
        btn.setOnClickListener(this);
        btn1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttondl:
                System.out.println("点击登陆按钮");
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(username=et1.getText().toString(), password=et2.getText().toString(), loginListener);
                saveSP();
                break;

            //break;
            case R.id.buttonzc:
                Intent intent1=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent1);
                break;

        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionid",sessionid);
        editor.putString("realname",realname);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交保存
    }
}